﻿using System;
using System.Linq;
using RpcClient;
using RpcHelper;
using RpcModel;
using Wedonek.RpcStore.Gateway.Interface;
using Wedonek.RpcStore.Gateway.Model;
using Wedonek.RpcStore.Service.Interface;
using Wedonek.RpcStore.Service.Model;

namespace Wedonek.RpcStore.Gateway.Service
{
    internal class ResourceModularService : IResourceModularService
    {
        private readonly IResourceModularCollect _Modular;
        private readonly IServerTypeCollect _ServerType;

        public ResourceModularService(IResourceModularCollect modular,
                IServerTypeCollect serverType)
        {
            this._ServerType = serverType;
            this._Modular = modular;
        }
        public BasicModular[] Gets(long systemTypeId)
        {
            return this._Modular.Gets(systemTypeId);
        }

        public ResourceModularData[] Query(ModularQueryParam query, IBasicPage paging, out long count)
        {
            ResourceModular[] list = this._Modular.Query(query, paging, out count);
            if (list.Length == 0)
            {
                return new ResourceModularData[0];
            }
            ServerType[] types = this._ServerType.Gets(list.Distinct(a => a.SystemType));

            return list.ConvertMap<ResourceModular, ResourceModularData>((a, b) =>
           {
               b.SystemType = types.Find(c => c.TypeVal == a.SystemType, c => c.SystemName);
               return b;
           });
        }

        public void SetRemark(Guid id, string remark)
        {
            this._Modular.SetRemark(id, remark);
        }
    }
}
